package com.schoolMall.controller.productCategoryAdmin;

import com.schoolMall.domain.ProductCategory;
import com.schoolMall.domain.Shop;
import com.schoolMall.dto.ProductCategoryDto;
import com.schoolMall.dto.ResultDto;
import com.schoolMall.enums.ProductCategoryStateEnum;
import com.schoolMall.exception.ProductCategoryException;
import com.schoolMall.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author:zyh
 * @Time:2021-08-31-19:31
 * @email:1269231889@qq.com
 */
@Controller
@RequestMapping("/schoolMall/proCateManage")
public class ProductCategoryManageController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @ResponseBody
    @GetMapping("/getProCate")
    public ResultDto<List<ProductCategory>> getProCate(HttpServletRequest request) {
        Shop shop = (Shop) request.getSession().getAttribute("currentShop");
        if (Objects.isNull(shop) || Objects.isNull(shop.getShopId())) {
            ResultDto<List<ProductCategory>> listResult = new ResultDto<>
                    (ProductCategoryStateEnum.NULL_SHOP.getMsg(), ProductCategoryStateEnum.NULL_SHOP.getVal());
            return listResult;
        }
        List<ProductCategory> productCategories = productCategoryService.queryProductCategory(shop.getShopId());
        ResultDto<List<ProductCategory>> resultDto = new ResultDto<>(productCategories);
        return resultDto;
    }

    @ResponseBody
    @PostMapping(value = "/addProdCate")
    public Map<String, Object> addProdCate(@RequestBody List<ProductCategory> productCategoryList, HttpServletRequest request) {
        Map<String, Object> model = new HashMap<>();
        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
        if (CollectionUtils.isEmpty(productCategoryList)) {
            model.put("success", false);
            model.put("msg", "请输入一个商品类别");
            return model;
        }
        productCategoryList.stream().filter(productCategory -> Objects.nonNull(productCategory)).
                forEach(productCategory -> productCategory.setShopId(currentShop.getShopId()));
        try {
            ProductCategoryDto productCategoryDto = productCategoryService.batchInsertProductCategory(productCategoryList);
            if (productCategoryDto.getProductCategoryStateEnum() == ProductCategoryStateEnum.SUCCESS) {
                model.put("success", true);
                model.put("msg", productCategoryDto.getProductCategoryList());
            } else {
                model.put("success", false);
                model.put("msg", productCategoryDto.getProductCategoryStateEnum().getMsg());
            }
        } catch (ProductCategoryException e) {
            model.put("success", false);
            model.put("msg", e.toString());
        }
        return model;
    }

    @ResponseBody
    @PostMapping("/deleteProductCategory")
    public Map<String, Object> deleteProductCategory(Long productCategoryId, Long shopId) {
        Map<String, Object> model = new HashMap<>();
        if (Objects.isNull(productCategoryId) || Objects.isNull(shopId)) {
            model.put("success", false);
            model.put("msg", "请输入一个商品类别id和商铺id");
            return model;
        }
        ProductCategoryDto productCategoryDto = null;
        try {
            productCategoryDto = productCategoryService.deleteProductCategory(productCategoryId, shopId);
        } catch (ProductCategoryException e) {
            model.put("success", false);
            model.put("msg", "删除失败");
            return model;
        }
        if (Objects.nonNull(productCategoryDto) && productCategoryDto.getProductCategoryStateEnum() == ProductCategoryStateEnum.SUCCESS) {
            model.put("success", true);
            model.put("msg", productCategoryDto);
        } else {
            model.put("success", false);
            model.put("msg", productCategoryDto);
        }
        return model;
    }
}
